#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* replaceSpaces(char* S, int length)
{
    int i = 0;
    for (i = 0; i < length; i++)
    {
        if (S[i] == ' ')
        {
            //将后面的字符向后移动两位。
            int j = length - 1;
            for (j = length - 1; j > i; j--)
            {
                S[j + 2] = S[j];
            }

            S[i++] = '%';
            S[i++] = '2';
            S[i] = '0';

            length += 2;
        }
    }
    char* ret = (char*)malloc(sizeof(char) * (length));
    strncpy(ret, S, length);
    printf("%d\n", length);
    printf("%d\n", (int)strlen(ret));

    printf("%s\n", ret);
    return ret;
}

int main()
{
    //char s[18] = "Mr John Smith";
    //char s[16] = "     ";
    char s[37] = "ds sdfs afs sdfa dfssf asdf";
    char* ret = replaceSpaces(s, 27);
    printf("%d\n", (int)strlen(ret));
    printf("%s\n", ret);
    return 0;
}